﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Com.Rivyer.Model;
namespace Com.Rivyer.BLL
{
	/// <summary>
	/// tmppingstatusBll
	/// </summary>
	public partial class tmppingstatusBll
	{
		private readonly Com.Rivyer.DAL.tmppingstatusDal dal=new Com.Rivyer.DAL.tmppingstatusDal();
        private readonly Com.Rivyer.BLL.tmppinglistBll pinglistbll = new tmppinglistBll();
		public tmppingstatusBll()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			return dal.Exists(id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(Com.Rivyer.Model.tmppingstatusModel model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Com.Rivyer.Model.tmppingstatusModel model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Com.Rivyer.Model.tmppingstatusModel GetModel(int id)
		{
			
			return dal.GetModel(id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Com.Rivyer.Model.tmppingstatusModel GetModelByCache(int id)
		{
			
			string CacheKey = "tmppingstatusModelModel-" + id;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(id);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Com.Rivyer.Model.tmppingstatusModel)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Com.Rivyer.Model.tmppingstatusModel> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Com.Rivyer.Model.tmppingstatusModel> DataTableToList(DataTable dt)
		{
			List<Com.Rivyer.Model.tmppingstatusModel> modelList = new List<Com.Rivyer.Model.tmppingstatusModel>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Com.Rivyer.Model.tmppingstatusModel model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Com.Rivyer.Model.tmppingstatusModel();
					if(dt.Rows[n]["id"]!=null && dt.Rows[n]["id"].ToString()!="")
					{
						model.id=int.Parse(dt.Rows[n]["id"].ToString());
					}
					if(dt.Rows[n]["username"]!=null && dt.Rows[n]["username"].ToString()!="")
					{
					model.username=dt.Rows[n]["username"].ToString();
					}
					if(dt.Rows[n]["description"]!=null && dt.Rows[n]["description"].ToString()!="")
					{
					model.description=dt.Rows[n]["description"].ToString();
					}
					if(dt.Rows[n]["ip"]!=null && dt.Rows[n]["ip"].ToString()!="")
					{
					model.ip=dt.Rows[n]["ip"].ToString();
					}
					if(dt.Rows[n]["pingstatus"]!=null && dt.Rows[n]["pingstatus"].ToString()!="")
					{
						model.pingstatus=int.Parse(dt.Rows[n]["pingstatus"].ToString());
					}
					if(dt.Rows[n]["pingdescription"]!=null && dt.Rows[n]["pingdescription"].ToString()!="")
					{
					model.pingdescription=dt.Rows[n]["pingdescription"].ToString();
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

        public void DeleteByUsername(string username)
        {
            System.Data.DataSet pingstatuslist = dal.GetList(string.Format("username = '{0}'", username));
            if (pingstatuslist.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < pingstatuslist.Tables[0].Rows.Count; i++)
                {
                    Delete(int.Parse(pingstatuslist.Tables[0].Rows[i]["id"].ToString()));
                }
            }
        }

		#endregion  Method

        public void PingStatus(string username, string packagebag)
        {
            //PING写入,拿到pinglist里面所有值
            List<Model.tmppinglistModel> listmodels = pinglistbll.GetModelList(string.Format("username = '{0}'", username));
            string result;
            DateTime dt;
            int package = int.Parse(packagebag);
            foreach (Model.tmppinglistModel listmodel in listmodels)
            {
                if (listmodel.pingstatus == 1)
                {
                    dt = DateTime.Now;
                Model.tmppingstatusModel statusmodel = new tmppingstatusModel();
                result = Maticsoft.Common.netping.pingIPcmd(listmodel.ip, package);
                listmodel.latestpingdate = dt;
                if (result.Contains("最短") || result.Contains("最长") || result.Contains("平均"))
                {
                    listmodel.pingstatus = 1;
                }
                else
                {
                    listmodel.pingstatus = 0;
                }
                statusmodel.description = listmodel.description;
                statusmodel.ip = listmodel.ip;
                statusmodel.pingdescription = result;
                if (result.Contains("最短") || result.Contains("最长") || result.Contains("平均"))
                {
                    statusmodel.pingstatus = 1;
                }
                else
                {
                    statusmodel.pingstatus = 0;
                }
                statusmodel.username = username;
                {
                    pinglistbll.Update(listmodel);
                    Add(statusmodel);
                }
                }
            }
        }

        public void PingStatus(string username, string packagebag, System.Text.StringBuilder sb)
        {
            //PING写入,拿到pinglist里面符合ID在sb里面字符串的值
            List<Model.tmppinglistModel> listmodels = pinglistbll.GetModelList(string.Format("id in ({0}) and username='{1}'", sb.ToString(),username));
            string result;
            DateTime dt;
            foreach (Model.tmppinglistModel listmodel in listmodels)
            {
                dt = DateTime.Now;
                Model.tmppingstatusModel statusmodel = new tmppingstatusModel();
                result = Maticsoft.Common.netping.pingIPcmd(listmodel.ip, int.Parse(packagebag));
                listmodel.latestpingdate = dt;
                if (result.Contains("最短") || result.Contains("最长") || result.Contains("平均"))
                {
                    listmodel.pingstatus = 1;
                }
                else
                {
                    listmodel.pingstatus = 0;
                }
                statusmodel.description = listmodel.description;
                statusmodel.ip = listmodel.ip;
                statusmodel.pingdescription = result;
                if (result.Contains("最短") || result.Contains("最长") || result.Contains("平均"))
                {
                    statusmodel.pingstatus = 1;
                }
                else
                {
                    statusmodel.pingstatus = 0;
                }
                statusmodel.username = username;
                {
                    pinglistbll.Update(listmodel);
                    Add(statusmodel);
                }
            }
        }
    }
}

